package com.project.core.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 时间转换工具类
 *
 * @version 1.0
 * @since 1.0
 */
public class DateUtil {

    public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATE_YEAR_PATTERN = "yyyy-MM";
    public static final String DATE_MONTH_PATTERN = "MM-dd";
    public static final String TIME_PATTERN = "HH:mm:ss";
    public static final String TIME_MINUTE_PATTERN = "HH:mm";
    public static final String DATETIME_NUM_PATTERN = "yyyyMMddHHmmss";
    public static final String DATE_NUM_PATTERN = "yyyyMMdd";
    public static final String TIME_NUM_PATTERN = "HHmmss";


    public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern(DATE_PATTERN);
    public static final DateTimeFormatter DATETIME_FORMAT = DateTimeFormatter.ofPattern(DATETIME_PATTERN);
    public static final DateTimeFormatter DATE_YEAR_FORMAT = DateTimeFormatter.ofPattern(DATE_YEAR_PATTERN);
    public static final DateTimeFormatter DATETIME_MINUTE_FORMAT = DateTimeFormatter.ofPattern(DATETIME_MINUTE_PATTERN);
    public static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern(TIME_PATTERN);
    public static final DateTimeFormatter TIME_MINUTE_FORMAT = DateTimeFormatter.ofPattern(TIME_MINUTE_PATTERN);
    public static final DateTimeFormatter DATE_MONTH_FORMAT = DateTimeFormatter.ofPattern(DATE_MONTH_PATTERN);
    public static final DateTimeFormatter DATETIME_NUM_FORMAT = DateTimeFormatter.ofPattern(DATETIME_NUM_PATTERN);
    public static final DateTimeFormatter DATE_NUM_FORMAT = DateTimeFormatter.ofPattern(DATE_NUM_PATTERN);
    public static final DateTimeFormatter TIME_NUM_FORMAT = DateTimeFormatter.ofPattern(TIME_NUM_PATTERN);

    /**
     * 时间字符串转换为 dateTime
     *
     * @param str 2024-01-01 12:12:12
     * @return LocalDateTime
     */
    public static LocalDateTime parseDateTime(String str) {
        return LocalDateTime.parse(str, DATETIME_FORMAT);
    }

    /**
     * 时间字符串转换为 date
     *
     * @param str 2024-01-01
     * @return LocalDate
     */
    public static LocalDate parseDate(String str) {
        return LocalDate.parse(str, DATE_FORMAT);
    }

    /**
     * 时间字符串转换为 time
     *
     * @param str 12:12:12
     * @return LocalTime
     */
    public static LocalTime parseTime(String str) {
        return LocalTime.parse(str, TIME_MINUTE_FORMAT);
    }

    /**
     * 将时间类型转换未字符串
     *
     * @param dateTime  2024-01-01 12:12:12
     * @param formatter DateTimeFormatter
     * @return String
     */
    public static String format(LocalDateTime dateTime, DateTimeFormatter formatter) {
        return formatter.format(dateTime);
    }

    /**
     * 将字符转转为时间类型
     *
     * @param dateTime  2024-01-01 12:12:12
     * @param formatter DateTimeFormatter
     * @return LocalDateTime
     */
    public static LocalDateTime parse(String dateTime, DateTimeFormatter formatter) {
        return LocalDateTime.parse(dateTime, formatter);
    }

    /**
     * 获取当前时间，并按照格式要求返回字符串
     *
     * @param formatter DateTimeFormatter
     * @return String
     */
    public static String GetCurrent(DateTimeFormatter formatter) {
        return LocalDateTime.now().format(formatter);
    }


    /**
     * 返回当前日期和时间字符串
     *
     * @return String
     */
    public static String getCurrentDateTime() {
        return DATETIME_FORMAT.format(LocalDateTime.now());
    }

    /**
     * 返回当前日期字符串
     *
     * @return String
     */
    public static String getCurrentDate() {
        return DATE_FORMAT.format(LocalDate.now());
    }

    /**
     * 返回当前时间字符串
     *
     * @return String
     */
    public static String getCurrentTime() {
        return TIME_MINUTE_FORMAT.format(LocalTime.now());
    }

    /**
     * 返回当前时间的数字串
     *
     * @return 20240301
     */
    public static String getCurrentDateNum() {
        return DATE_NUM_FORMAT.format(LocalDate.now());
    }
}
